package emulator.shell.GUI.symbols;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import emulator.assembler.SymbolTableSource;

@SuppressWarnings("serial")
public class SymbolSourceTableModel extends AbstractTableModel
{
	private static String[] columns = { "", "Source" };
	private List<SymbolSourceModel> data;

	public SymbolSourceTableModel(List<SymbolSourceModel> model)
	{
		this.data = model;
	}
	
	@Override
	public String getColumnName(int column)
	{
		return columns[column];
	}
	
	@Override
	public int getRowCount() {
		return data.size();
	}

	@Override
	public int getColumnCount() {
		return columns.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		switch (columnIndex)
		{
		case 0: return data.get(rowIndex).isSelected();
		case 1: return data.get(rowIndex).getSourceName();
		}
		return null;
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		if (columnIndex == 0)
			data.get(rowIndex).setSelected((Boolean)aValue);
		else
			super.setValueAt(aValue, rowIndex, columnIndex);
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return column != 1;
	}

	@Override
	public Class<?> getColumnClass(int col) {
		return getValueAt(0, col).getClass();
	}

	public Collection<SymbolTableSource> getSelectedSources()
	{
		List<SymbolTableSource> selectedSources = new ArrayList<SymbolTableSource>();
		for (SymbolSourceModel sourceModel : data)
		{
			if (sourceModel.isSelected())
				selectedSources.add(sourceModel.getSource());
		}
		return selectedSources;
	}

}
